package com.yilizhiyou.controller;

import com.yilizhiyou.common.Result;
import com.yilizhiyou.service.AiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * AI服务控制器
 * 
 * @author 伊犁智游开发团队
 * @since 1.0.0
 */
@RestController
@RequestMapping("/ai")
@Tag(name = "AI服务", description = "语音识别、语音合成、图像识别、智能问答等AI服务接口")
public class AiController {

    private static final Logger log = LoggerFactory.getLogger(AiController.class);
    private final AiService aiService;

    public AiController(AiService aiService) {
        this.aiService = aiService;
    }

    @PostMapping("/speech-to-text")
    @Operation(summary = "语音识别", description = "将语音转换为文本")
    public Result<String> speechToText(
            @Parameter(description = "音频文件") @RequestParam("audio") MultipartFile audioFile,
            @Parameter(description = "语言") @RequestParam(defaultValue = "zh-CN") String language) {
        
        try {
            byte[] audioData = audioFile.getBytes();
            String result = aiService.speechToText(audioData, language);
            return Result.success("语音识别成功", result);
        } catch (Exception e) {
            log.error("语音识别失败", e);
            return Result.error("语音识别失败: " + e.getMessage());
        }
    }

    @PostMapping("/text-to-speech")
    @Operation(summary = "语音合成", description = "将文本转换为语音")
    public Result<byte[]> textToSpeech(
            @Parameter(description = "文本内容") @RequestParam String text,
            @Parameter(description = "语言") @RequestParam(defaultValue = "zh-CN") String language,
            @Parameter(description = "语音类型") @RequestParam(defaultValue = "female") String voiceType) {
        
        try {
            byte[] audioData = aiService.textToSpeech(text, language, voiceType);
            return Result.success("语音合成成功", audioData);
        } catch (Exception e) {
            log.error("语音合成失败", e);
            return Result.error("语音合成失败: " + e.getMessage());
        }
    }

    @PostMapping("/image-recognition")
    @Operation(summary = "图像识别", description = "识别图像中的物体和场景")
    public Result<Map<String, Object>> imageRecognition(
            @Parameter(description = "图像文件") @RequestParam("image") MultipartFile imageFile) {
        
        try {
            byte[] imageData = imageFile.getBytes();
            Map<String, Object> result = aiService.imageRecognition(imageData);
            return Result.success("图像识别成功", result);
        } catch (Exception e) {
            log.error("图像识别失败", e);
            return Result.error("图像识别失败: " + e.getMessage());
        }
    }

    @PostMapping("/crowd-detection")
    @Operation(summary = "人流检测", description = "检测图像中的人数")
    public Result<Map<String, Object>> crowdDetection(
            @Parameter(description = "图像文件") @RequestParam("image") MultipartFile imageFile) {
        
        try {
            byte[] imageData = imageFile.getBytes();
            Map<String, Object> result = aiService.crowdDetection(imageData);
            return Result.success("人流检测成功", result);
        } catch (Exception e) {
            log.error("人流检测失败", e);
            return Result.error("人流检测失败: " + e.getMessage());
        }
    }

    @PostMapping("/qa")
    @Operation(summary = "智能问答", description = "基于上下文的智能问答")
    public Result<String> intelligentQa(
            @Parameter(description = "问题") @RequestParam String question,
            @Parameter(description = "上下文") @RequestParam(required = false) String context) {
        
        try {
            String answer = aiService.intelligentQa(question, context);
            return Result.success("智能问答成功", answer);
        } catch (Exception e) {
            log.error("智能问答失败", e);
            return Result.error("智能问答失败: " + e.getMessage());
        }
    }

    @PostMapping("/sentiment-analysis")
    @Operation(summary = "情感分析", description = "分析文本的情感倾向")
    public Result<Map<String, Object>> sentimentAnalysis(
            @Parameter(description = "文本内容") @RequestParam String text) {
        
        try {
            Map<String, Object> result = aiService.sentimentAnalysis(text);
            return Result.success("情感分析成功", result);
        } catch (Exception e) {
            log.error("情感分析失败", e);
            return Result.error("情感分析失败: " + e.getMessage());
        }
    }

    @PostMapping("/translation")
    @Operation(summary = "文本翻译", description = "翻译文本内容")
    public Result<String> textTranslation(
            @Parameter(description = "文本内容") @RequestParam String text,
            @Parameter(description = "源语言") @RequestParam(defaultValue = "zh-CN") String sourceLanguage,
            @Parameter(description = "目标语言") @RequestParam(defaultValue = "en-US") String targetLanguage) {
        
        try {
            String translation = aiService.textTranslation(text, sourceLanguage, targetLanguage);
            return Result.success("文本翻译成功", translation);
        } catch (Exception e) {
            log.error("文本翻译失败", e);
            return Result.error("文本翻译失败: " + e.getMessage());
        }
    }

    @PostMapping("/nlp")
    @Operation(summary = "自然语言处理", description = "通用自然语言处理接口")
    public Result<Map<String, Object>> naturalLanguageProcessing(
            @Parameter(description = "文本内容") @RequestParam String text,
            @Parameter(description = "任务类型") @RequestParam String task) {
        
        try {
            Map<String, Object> result = aiService.naturalLanguageProcessing(text, task);
            return Result.success("自然语言处理成功", result);
        } catch (Exception e) {
            log.error("自然语言处理失败", e);
            return Result.error("自然语言处理失败: " + e.getMessage());
        }
    }
}
